var BridgeOnloading = false;
var HMJsBridge = {};
var readyCallbackQuene = [];

function signatrue(env, callback) {
	env = env == '' || env == null || env == 'production' ? '' : ('-' + env);
	var signatrueUrl = 'https://h5.laicunba.com/h5/ext/activity/mi_signature.htm';

	var url = window.location.href.replace(window.location.hash, '');
	var xhr = new XMLHttpRequest();
	xhr.open("get", signatrueUrl + 'url=' + encodeURIComponent(url), false);
	var that = this;
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && ('' + xhr.status).charAt(0) == 2) {
			var data = eval('(' + xhr.responseText + ')');
			typeof callback === 'function' && callback(data)
		}
	}
	xhr.send();
}

function readyCallbacksDo(HM_JsBridge){
	for (var i = readyCallbackQuene.length - 1; i >= 0; i--) {
		readyCallbackQuene.pop()(HM_JsBridge);
	}
}

function HMJsBridgeReady(callback) {
	readyCallbackQuene.push(callback);
	if (window.HM_JsBridge) {
		if (BridgeOnloading) {
			readyCallbacksDo(HM_JsBridge)
		} else {
			BridgeOnloading = true;
			window.HM_JsBridge.init();
			readyCallbacksDo(HM_JsBridge)
		}
	} else {
		if (!BridgeOnloading) {
			document.addEventListener(
			 'HM_JsBridgeReady',
			 function() {
				BridgeOnloading = true;
				window.HM_JsBridge.init();
				readyCallbacksDo(HM_JsBridge)
			 },
			 false
			);
		}
	}
}

var isSupport = function() {
	var matched = navigator.userAgent.match(/com\.xiaomi\.hm\.health\/(\d+)?\_?((\d+)\.?(\d+)?\.?(\d+)?)/);
	if(matched && matched.length === 6 ){
	return	Number(matched[3]) * 1000 + Number(matched[4]) * 10 + Number(matched[5]) >= 2090;
	} else {
	return false;
	}
}

var jsbridgeToPageCallback = function(responseData, callback) {
	var message = null;
	try {
		message = JSON.parse(responseData);
	} catch(e) {
		console.log(e);
	}
	typeof callback === 'function' && callback(message, HMJsBridge)
}

var preVerifyJsApi = function(requestData, callback) {
	var env = requestData.env ? requestData.env : '';
	signatrue(env, function(data) {
		data.jsApiList = requestData.jsApiList;
		HM_JsBridge.invoke('preVerifyJsApi', JSON.stringify(data), function(responseData) {
			jsbridgeToPageCallback(responseData, callback)
		});
	})
}

var getLocation = function(callback) {
	HM_JsBridge.invoke('getLocation', null, function(responseData) {
		jsbridgeToPageCallback(responseData, callback)
	});
}

var openInBrowser = function(param, callback) {
	HM_JsBridge.invoke('openInBrowser', param, function(responseData) {
		jsbridgeToPageCallback(responseData, callback)
	});
}

HMJsBridge.isSupport = isSupport;
HMJsBridge.preVerifyJsApi = preVerifyJsApi;
HMJsBridge.getLocation = getLocation;
HMJsBridge.openInBrowser = openInBrowser;
HMJsBridge.HMJsBridgeReady = HMJsBridgeReady;

if (typeof module !== 'undefined' && typeof exports === 'object') {
	module.exports = HMJsBridge;
} else if (typeof define === 'function' && (define.amd || define.cmd)) {
	define(function() {
		return HMJsBridge;
	});
} else {
	this.HMJsBridge = HMJsBridge;
}